package com.bestvike.stone.core.threading.inherit;

import com.bestvike.stone.core.threading.WaitCallable;
import com.bestvike.stone.core.threading.holder.RawThreadLocalsHolder;
import org.springframework.util.Assert;

import java.util.concurrent.Callable;

/**
 * Created by 许崇雷 on 2018/8/13.
 */
public final class InheritWaitCallable<TState, TResult> extends InheritAction implements Callable<TResult> {
    private final WaitCallable<TState, TResult> waitCallback;
    private final TState state;

    public InheritWaitCallable(WaitCallable<TState, TResult> waitCallback, TState state) {
        Assert.notNull(waitCallback, "waitCallback can not be null");
        this.waitCallback = waitCallback;
        this.state = state;
    }

    @Override
    public TResult call() {
        try (RawThreadLocalsHolder ignored = this.ready()) {
            return this.waitCallback.call(this.state);
        }
    }
}
